package com.example.springboottest.controller;

import com.example.springboottest.entity.MyLock;
import com.example.springboottest.repository.MyLockRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/lock")
public class MyLockController {

    @Autowired
    MyLockRepository myLockRepository;

    @GetMapping("/findAll/{pageNo}/{pageSize}")
    public Page<MyLock> findAll(@PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        return myLockRepository.findAll(pageable);
    }

    @PostMapping("/save")
    public String save(@RequestBody MyLock myLock) {
        MyLock result = myLockRepository.save(myLock);
        if (result != null) {
            return "success";
        } else {
            return "error";
        }
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        myLockRepository.deleteById(id);
        return "success";
    }
}
